<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/connopen.asp"-->
<!--#include file="lib/asp-common-lib.asp"-->
<!--#include file="lib/asp-json-lib.asp"-->
<%
Response.ContentType = "application/json"
action=saferequest("action",0)
id=saferequest("id",1)
todo_id=saferequest("todo_id",1)
cd=saferequest("cd",0)
if not IsNumeric(cd) then cd=""
userid=session("iId")
todo_title=saferequest("todo_title",0)
todo_star=saferequest("todo_star",0)
if todo_star<>"" then todo_star=True else todo_star=False
todo_content=saferequest("todo_content",0)
todo_active=saferequest("todo_active",0)


set json=jsObject()
json("bStatus")=False
json("sInfo")=""

if action="保存" and todo_id=0 then
    
    if not CheckEmployee() then 
        json("sInfo")="您不是员工，不能新建任务"
    else
        if todo_active<>"" then
            sql="insert into todo(userid,title,todo,star,active_date) values("&userid&",'"&todo_title&"','"&todo_content&"',"&todo_star&",'"&todo_active&"')"
        else
            sql="insert into todo(userid,title,todo,star,active_date) values("&userid&",'"&todo_title&"','"&todo_content&"',"&todo_star&",null)"
        end if
        ' sql="insert into todo(userid,title,todo,star,active_date) values("&userid&",'"&todo_title&"','"&todo_content&"',"&todo_star&",'"&todo_active&"')"
        todoid=ExecuteCommand(sql,True)
        if cd<>"" then 
            cdtodo_sql="insert into cdtodo(cd,todo) values("&cd&","&todoid&")"
            call ExecuteCommand(cdtodo_sql,False)
        end if
        ' if todo_active<>"" then call ExecuteCommand("update todo set active_date='"&todo_active&"' where id="&todoid,False)
        call ExecuteCommand("update todo set rank="&todoid&" where id="&todoid,False)
        json("bStatus")=True
        json("sInfo")="新建任务成功"
    end if
end if
if action="保存" and todo_id<>0 then
    
    if not CheckEmployee() then 
        json("sInfo")="您不是员工，不能编辑任务"
    else
        if todo_active<>"" then
            sql="update todo set title='"&todo_title&"',todo='"&todo_content&"',star="&todo_star&",active_date='"&todo_active&"',edit_datetime=now() where id="&todo_id
        else
            sql="update todo set title='"&todo_title&"',todo='"&todo_content&"',star="&todo_star&",active_date=null,edit_datetime=now() where id="&todo_id
        end if
        call ExecuteCommand(sql,False)
        json("bStatus")=True
        json("sInfo")="编辑任务成功"
    end if
end if



if action="完成" then
    
    sql="update todo set complete=true,complete_datetime=now() where id="&id
    call ExecuteCommand(sql,false)
    json("bStatus")=True
    json("sInfo")="任务完成"
end if
if action="重启" then
    sql="update todo set complete=false,complete_datetime=null,edit_datetime=now() where id="&id
    call ExecuteCommand(sql,false)
    json("bStatus")=True
    json("sInfo")="任务重启"
end if
if action="删除" then
    sql="update todo set deleted=true where id="&id
    call ExecuteCommand(sql,false)
    json("bStatus")=True
    json("sInfo")="任务删除"
end if
if action="编辑" then
    sql="select * from todo where id="&id
    set rs = Server.CreateObject("ADODB.Recordset")
    rs.open sql,conn,1,1
    if not rs.eof then
        json("iTodoID")=rs("id")
        json("sTodoTitle")=rs("title")
        json("sTodoContent")=rs("todo")
        json("sTodoActive")=rs("active_date")
        json("bTodoStar")=rs("star")
        json("bStatus")=True
    else
        json("sInfo")="读取任务错误"
    end if
    rs.close
    set rs=nothing
end if
json.Flush

%>
<!--#include file="lib/connclose.asp"-->